package xgenerator.ui.swing.adapter;

import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import xgenerator.ui.swing.CreateDataSourcesDialog;
import xgenerator.ui.swing.DataSourceManagerDialog;

public class DataSourceAdapter extends AbstractAction {
	
	/**
	 * Serializable
	 */
	private static final long serialVersionUID = 1L;
	
	public static final String MANAGER_DATASOURCES_COMMAND = "manager";
	public static final String QUERY_DATASOURCES_COMMAND = "query";
	public static final String CREATE_DATASOURCES_COMMAND = "create";
	public static final String UPDATE_DATASOURCES_COMMAND = "update";
	public static final String DELETE_DATASOURCES_COMMAND = "delete";
	public static final String VIEW_DATASOURCES_COMMAND = "view";

	/**
	 * UI组件
	 */
	private Component ui;
	
	/**
	 * 创建数据源dialog
	 */
	private CreateDataSourcesDialog createDataSourcesDialog;

	/**
	 * constructor
	 * @param ui
	 */
	public DataSourceAdapter(Component ui) {
		this.ui = ui;
	}

	/**
	 * <p>
	 * Title：actionPerformed
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if(MANAGER_DATASOURCES_COMMAND.equals(e.getActionCommand())) {
			managerDataSourceActionPerformed(e);
		} else if(QUERY_DATASOURCES_COMMAND.equals(e.getActionCommand())) {
			queryDataSourceActionPerformed(e);
		} else if(CREATE_DATASOURCES_COMMAND.equals(e.getActionCommand())) {
			createDataSourceActionPerformed(e);
		} else if(UPDATE_DATASOURCES_COMMAND.equals(e.getActionCommand())) {
			updateDataSourceActionPerformed(e);
		} else if(DELETE_DATASOURCES_COMMAND.equals(e.getActionCommand())) {
			deleteDataSourceActionPerformed(e);
		} else if(VIEW_DATASOURCES_COMMAND.equals(e.getActionCommand())) {
			viewDataSourceActionPerformed(e);
		}
	}
	
	/**
	 * <p>
	 * Title：数据源管理对话框
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void managerDataSourceActionPerformed(ActionEvent e) {
		new DataSourceManagerDialog((Frame) ui, "", true);
	}
	
	/**
	 * <p>
	 * Title：查询数据源
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void queryDataSourceActionPerformed(ActionEvent e) {
		
	}
	
	/**
	 * <p>
	 * Title：创建数据源
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void createDataSourceActionPerformed(ActionEvent e) {
		if(null == createDataSourcesDialog) {
			this.createDataSourcesDialog = new CreateDataSourcesDialog((Dialog)ui, true);
		}
		if(!createDataSourcesDialog.isVisible()) {
			this.createDataSourcesDialog.setVisible(true);
		}
	}
	
	/**
	 * <p>
	 * Title：更新数据源
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void updateDataSourceActionPerformed(ActionEvent e) {
		new DataSourceManagerDialog((Frame) ui, "", true);
	}
	
	/**
	 * <p>
	 * Title：删除数据源
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void deleteDataSourceActionPerformed(ActionEvent e) {
		
	}
	
	/**
	 * <p>
	 * Title：查看数据源
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void viewDataSourceActionPerformed(ActionEvent e) {
		
	}

	/**
	 * <p>
	 * Title：获取ui界面
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	public Component getUI() {
		return ui;
	}

	/**
	 * <p>
	 * Title：设置ui界面
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param ui
	 */
	public void setUI(Component ui) {
		this.ui = ui;
	}

}
